#include					"encoder.h"
#include					"byte.h"
#include					<iostream>

Encoder::Encoder() : _enc(NULL), _channel(2)
{

}

Encoder::~Encoder()
{
	if (_enc != NULL)
		opus_encoder_destroy(_enc);
}

void						Encoder::start()
{
	int						error = 0;
	if (_enc != NULL)
		opus_encoder_destroy(_enc);
	_enc = opus_encoder_create(48000, _channel, OPUS_APPLICATION_AUDIO, &error);
	if (error != OPUS_OK)
		std::cout << "error in opus_encoder_create" << std::endl;
}

void						Encoder::encode(SoundPacket *packet, Packet *order)
{
	short					size;
	byte					*output = new byte[PACKET_SIZE];

	size = opus_encode(_enc, packet->data(), packet->frame_size(), output, PACKET_SIZE);

	order->pushData<short>(&size);
	order->pushData<byte>(output, size);
	delete output;
}
